package com.yoo.ons

import android.app.Activity
import android.app.Application
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.yoo.module_base.utils.LogUtil

/**
 * @ClassName: ProcessObserver
 * @Author: You
 */
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class ProcessObserver(var application: Application) : LifecycleObserver {
    companion object {
        var currentActivity: Activity? = null

        //前台
        const val STATE_FOREGROUND = 0

        //后台
        const val STATE_BACKGROUND = 1

        //当前状态
        var currentState = STATE_FOREGROUND

    }

    private val TAG = "ProcessObserver"

    init {
        application.registerActivityLifecycleCallbacks(object :
            Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

            }

            override fun onActivityStarted(activity: Activity) {
                LogUtil.debugLog(TAG, "类名：${activity.javaClass.simpleName}")
                currentActivity = activity
            }

            override fun onActivityResumed(activity: Activity) {

            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityDestroyed(activity: Activity) {

            }
        })
    }

    //onStart时间
    var startTime = 0L

    private fun onCreate() {
       //冷启动
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private fun onAppForeground() {
        // 应用进入前台
        if (startTime != 0L) {
           //热启动
        }
        startTime = System.currentTimeMillis()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private fun onAppBackground() {
        // 应用进入后台
//        DotLogUtil.getInstance().setEventName(EventName.QUIT_APP_WAY)
//            .add("type", "home键退出")
//            .add("diff_time", "${abs(System.currentTimeMillis() - startTime) / 1000}")
//            .commit()
    }
}